﻿using MuseLibrary.Packets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MuseFromFile
{
    class Program
    {
        static void Main(string[] args)
        {
            _channelValues = new int[4];
            String fileString;
            using (var file = new StreamReader("c:\\users\\David\\desktop\\museoutput.csv"))
            {
                fileString = file.ReadToEnd();
            }

            var lines = fileString.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            var vals = new List<int>();
            _packets = new List<Packet>();
            foreach (var line in lines)
                vals.Add(Int32.Parse(line));

            var index = 0;
            while (vals.Count > index)
            {
                var b = vals[index];
                var packet = PacketFactory.MakePacket(b);
                if (packet == null)// || (_packets.Count < 3 && packet.Type != PacketType.Synchronisation))
                {
                    index++;
                    continue;
                }

                while (!packet.IsInitialised)
                {
                    packet.ReadByte(b);
                    if (index == vals.Count - 1)
                    {
                        continue;
                    }

                    b = vals[++index];
                }

                _packets.Add(packet);
                if (packet.HasPostInitialisation)
                {
                    packet.PostInitialisation(_channelValues);
                }
            }
        }

        private static List<Packet> _packets;
        private static int[] _channelValues;
    }
}
